#include "ReactorThreadPool.h"

namespace dsth {

ReactorThreadPool::ReactorThreadPool(int num) 
    : thread_num_(num), index_(0) {
    for (int i = 0; i < thread_num_; ++i) {
        pool_.push_back(new ReactorThread);
    }
}

ReactorThreadPool::~ReactorThreadPool() {
    for (int i = 0; i < thread_num_; ++i) {
        delete pool_[i];
    }
}

ReactorThreadPool* ReactorThreadPool::Run() {
    for (int i = 0; i < thread_num_; ++i) {
        pool_[i]->Run();
    }
    return this;
}

EventLoop* ReactorThreadPool::GetNextEventLoop() {
    EventLoop* loop = NULL;
    if (thread_num_ > 0) {
        loop = pool_[index_++]->GetEventLoop();
        if (index_ == thread_num_) index_ = 0;
    }
    return loop;
}

} // namespace
